package Ep01_Array.Feb25th_0013;

/**
 * @author Jimmy Zhan WORKSTATION
 * @Date 2023/2/27 8:40
 * 说明：13 罗马数字转整数
 * 倒序：
 */

public class Mine {
    public static void main(String[] args) {
        Mine m = new Mine();
        int res = m.romanToInt("III");
        System.out.println(res);
    }
    public int romanToInt(String s){

        int len = s.length();
        int preNum = getValue(s.charAt(len-1));
        int result = preNum;
        if (len == 1){
            return result;
        }
        for (int i = s.length()-2; i >= 0; i--) {
            int num = getValue(s.charAt(i));
            if (preNum > num){
                result -= num;
            } else if (preNum <= num){
                preNum = num;
                result += num;
            }
        }
        return result;
    }

    private int getValue(char ch){
        switch (ch){
            case 'I': return 1;
            case 'V': return 5;
            case 'X': return 10;
            case 'L': return 50;
            case 'C': return 100;
            case 'D': return 500;
            case 'M': return 1000;
            default: return 0;
        }
    }

}
